import { getVersion } from "../../api/mine";
export const version = '1.0.0'
export const brandMap = {
    'huawei': 1,
    'xiaomi': 2,
    'oppo': 3,
    'vivo': 4,
    'honor': 5
}
export const systemTypeMap = {
    'android': 1,
    'ios': 2
}
/**
 * systemType: 系统类型(1:安卓 2:苹果)
platformTypes: 平台类型(1: 华为 2:小米 3:OPPO 4:VIVO 5:荣耀)，数组
uploadType: 更新方式(1:强制更新 2:强提示 3:弱提示)
promptType: 提示频次(1:每次打开 2:每天一次 3:每周一次)
*/

// 对比版本
export const compareVersions = (version1, version2) => {
    const parts1 = version1.split('.').map(Number);
    const parts2 = version2.split('.').map(Number);

    for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
        const num1 = parts1[i] || 0;
        const num2 = parts2[i] || 0;

        if (num1 > num2) return true;
        if (num1 < num2) return false;
    }

    return false;
}

export const needUpdateApp = () => {
    return new Promise((resolve, reject) => {
        // 获取当前版本号
        const currentVersion = version;
        let brand = uni.getSystemInfoSync().brand
        let systemType = uni.getSystemInfoSync().platform;
        let platformTypes = brandMap[brand] || 0
        // 获取服务器版本号
        console.log('更新参数：',{
            systemType: systemTypeMap[systemType] || 1,
            platformTypes: [platformTypes],
        })
        getVersion({
            systemType: systemTypeMap[systemType] || 1,
            platformTypes: [platformTypes],
        }).then(res => {
            const serverVersion = res.data.versionNumber;
            // 需要跟新
            if (compareVersions(serverVersion, currentVersion)) {
                resolve(res.data)
            } else {
                resolve(false)
            }
        });

    })
}